package com.zwb.demo.easyweb.modules.config.service.impl;

import com.zwb.demo.easyweb.common.entity.response.CommonCode;
import com.zwb.demo.easyweb.common.entity.response.ResponseResult;
import com.zwb.demo.easyweb.core.exception.ExceptionCast;
import com.zwb.demo.easyweb.core.utils.ConvertUtils;
import com.zwb.demo.easyweb.modules.config.entity.GenTemplatePO;
import com.zwb.demo.easyweb.modules.config.repository.GenTemplatePoRepository;
import com.zwb.demo.easyweb.modules.config.service.GenTemplateFileService;
import com.zwb.demo.easyweb.modules.config.vo.GenTemplateFileVO;
import com.zwb.demo.easyweb.modules.config.vo.GenTemplateVO;
import com.zwb.demo.easyweb.modules.config.service.GenTemplateService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
public class GenTemplateServiceImpl implements GenTemplateService {

    private final GenTemplatePoRepository genTemplateRepository;
    private final GenTemplateFileService genTemplateFileService;

    @Autowired
    public GenTemplateServiceImpl(GenTemplatePoRepository genTemplateRepository, GenTemplateFileService genTemplateFileService) {
        this.genTemplateRepository = genTemplateRepository;
        this.genTemplateFileService = genTemplateFileService;
    }


    @Override
    @Transactional
    public ResponseResult saveOne(GenTemplateVO vo) {
        if (StringUtils.isNotBlank(vo.getId())) {
            Optional<GenTemplatePO> optional = genTemplateRepository.findById(vo.getId());
            if (!optional.isPresent()) {
                ExceptionCast.cast(CommonCode.ID_NO_EXITS);
            }
            GenTemplatePO po = optional.get();
            BeanUtils.copyProperties(vo, po);
            genTemplateRepository.save(po);
            for (GenTemplateFileVO fileVO : vo.getFiles()) {
                genTemplateFileService.saveOne(fileVO);
            }
        } else {
            GenTemplatePO po = ConvertUtils.convertObject(vo, GenTemplatePO.class);
            genTemplateRepository.save(po);
            for (GenTemplateFileVO fileVO : vo.getFiles()) {
                genTemplateFileService.saveOne(fileVO);
            }
        }
        return ResponseResult.ok();
    }

    @Override
    @Transactional
    public ResponseResult deleteOne(String uuid) {
        if (StringUtils.isBlank(uuid)) {
            ExceptionCast.cast(CommonCode.ILLEGAL_PARAM);
        }
        Optional<GenTemplatePO> optional = genTemplateRepository.findById(uuid);
        if (!optional.isPresent()){
            ExceptionCast.cast(CommonCode.ID_NO_EXITS);
        }
        genTemplateRepository.deleteById(uuid);
        genTemplateFileService.deleteAllByTemplateId(uuid);
        return ResponseResult.ok();
    }

    @Override
    public ResponseResult findAll() {
        List<GenTemplatePO> allPO = genTemplateRepository.findAll();
        List<GenTemplateVO> allVO = ConvertUtils.convertList(allPO, GenTemplateVO.class);
        return ResponseResult.ok().body(allVO);
    }
}
